package com.woan.api.services;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.woan.api.util.DateJsonValueProcessor;
import com.woan.api.vo.VOnevideo;
import com.woan.api.vo.VpositionList;
import com.woan.api.vo.VvideoList;
import com.woan.api.idao.SysAdmininfoIDAO;
import com.woan.api.idao.SysVideoIDAO;
import com.woan.api.model.SysAdmininfo;
import com.woan.api.model.SysVideo;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;



public class VideoServerServices {
	private SysAdmininfoIDAO sysadmininfoiDAO;
	private SysVideoIDAO sysvideoiDAO;
	public SysAdmininfoIDAO getSysadmininfoiDAO() {
		return sysadmininfoiDAO;
	}
	public void setSysadmininfoiDAO(SysAdmininfoIDAO sysadmininfoiDAO) {
		this.sysadmininfoiDAO = sysadmininfoiDAO;
	}
	/*
	 * 获取本组内正在直播的视频列表
	 */
	public SysVideoIDAO getSysvideoiDAO() {
		return sysvideoiDAO;
	}
	public void setSysvideoiDAO(SysVideoIDAO sysvideoiDAO) {
		this.sysvideoiDAO = sysvideoiDAO;
	}
	public List<VvideoList> listLiveVideos(String rid,String type,Integer beginIndex,Integer endIndex){
		List<VvideoList> video = null;
		video = sysvideoiDAO.FindVLiveVideoByGroup(rid,type, beginIndex, endIndex);
		return video;
	}
	public List<VvideoList> listHistoryVideos(String rid,String type,Integer beginIndex,Integer endIndex){
		List<VvideoList> video = null;
		video = sysvideoiDAO.FindVHistoryVideoByGroup(rid,type, beginIndex, endIndex);
		return video;
	}
	public VOnevideo listVideosByVid(String vid,String type)
	{
		VOnevideo video = sysvideoiDAO.FindVOneVideoByVid(vid,type);
		return video;
	}
	public int listHistoryVideosCount(String groupname,String username,String userpwd){
		
		SysAdmininfo user = null;
		user = sysadmininfoiDAO.FindUserByGroupNameAndUsername(groupname,username,userpwd);	
		return sysvideoiDAO.FindHistoryVideoCountByGroup(user.getSysRole().getRid(),"");
	}
	public List<VvideoList> searchVideos(String groupname,String username,String userpwd,String devicenum,Integer beginIndex,Integer endIndex,Map<String, String> map){
		
		SysAdmininfo user = null;
		user = sysadmininfoiDAO.FindUserByGroupNameAndUsername(groupname,username,userpwd);
		List<VvideoList> video = null;
		video = sysvideoiDAO.FindVSerachVideoByDetail(user.getSysRole().getRid(), beginIndex, endIndex,map);
		return video;
	}
	public JSONObject editVideos(String groupname,String username,String pwd,String devicenum,String videoid,String videodetail){
		String flag="0";
		JSONObject json=new JSONObject();
		flag = sysvideoiDAO.EditVideos(videoid, videodetail);
		if(flag.equals("0"))
		{
			json.accumulate("results", 1);
			json.accumulate("errMsg", "修改成功");
		}
		else
		{
			json.accumulate("results", 0);
			json.accumulate("errMsg", "修改失败");
		}
		return json;
	}
	public JSONObject deleteVideos(String groupname,String username,String pwd,String devicenum,String videoid){
		boolean flag = true;
		if(!videoid.equals("") && videoid!=null)
		{
			String []arrayvideo = videoid.split(",");
			for (int i = 0 ;i<arrayvideo.length;i++)
			{
				if(arrayvideo[i]!=null && !arrayvideo[i].equals(""))
					flag=sysvideoiDAO.DeleteVideos(arrayvideo[i]);
			}
		}
		JSONObject json=new JSONObject();
		if(flag)
		{
			json.accumulate("results", 1);
			json.accumulate("errMsg", "删除成功");
		}
		else
		{
			json.accumulate("results", 0);
			json.accumulate("errMsg", "删除失败");
		}
		return json;
	}
	public List<VpositionList> listVideoPostion(String rid,String type){
		List<VpositionList> postion = null;
		postion = sysvideoiDAO.FindVVideoPostion(rid,type);
		return postion;
	}
	
}
